package cn.atzxc.qy.controller;

import cn.atzxc.qy.domain.Order;
import cn.atzxc.qy.domain.ResponseResult;
import cn.atzxc.qy.domain.vo.OrderVo;
import cn.atzxc.qy.domain.vo.TaskVo;
import cn.atzxc.qy.service.OrderService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户发布任务思路
 * 1.在用户进入指定的任务发布界面，获取到该用户的用户ID
 *      然后通过用户ID（以及指定的使用条件）
 *      在优惠卷中间表 中查询出优惠卷的信息(可以使用的为亮色，不可以使用的为暗色)
 */
@RestController
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户订单创建（生成订单编号）
     * 此时只是点击了支付按钮，跳转到了用户的订单界面准备进行支付
     * @param token 用于身份验证，以及获取用户的 user_id
     * @return
     */
    @PostMapping("/insert")
    public ResponseResult orderCreate(@RequestHeader("token") String token,
                                      @RequestBody JSONObject requestData) throws Exception {
        JSONObject orderJson = requestData.getJSONObject("order");
        JSONObject taskJson = requestData.getJSONObject("taskVo");


        Order order = orderJson.toJavaObject(Order.class);
        TaskVo taskVo = taskJson.toJavaObject(TaskVo.class);


        System.out.println("获取的订单信息为："+order);
        System.out.println("获取的任务信息为："+taskVo);


        Boolean result = orderService.insertOrder(token, order, taskVo);
        if(result){
            return new ResponseResult(200, "接单成功");
        }else {
            return new ResponseResult(408, "接单失败，骑手未认证");
        }


    }
    //根据任务查询出指定的订单的详细信息
    @GetMapping("/selectOneByTaskId")
    public ResponseResult selectOneByTaskId(@RequestParam("taskid") Long taskid){
        System.out.println("接收到了任务ID："+taskid);
        OrderVo orederVo = orderService.selectOneByTaskId(taskid);
        System.out.println("获取到得vo:"+orederVo);
        return new ResponseResult(200,"查询成功",orederVo);
    }
    //根据订单ID 查询 订单的详细信息
    @GetMapping("/selectOneByUserId")
    public ResponseResult selectOneByUserId(@RequestParam("orderid") Long orderid){
        System.out.println("接收到了订单ID："+orderid);
        OrderVo orederVo = orderService.selectOneByUserId(orderid);
        System.out.println("获取到得vo:"+orederVo);
        return new ResponseResult(200,"查询成功",orederVo);
    }

    //根据用户ID，获取等待收货得数据
    @GetMapping("/selectWaitGet")
    public ResponseResult selectWaitGet(@RequestHeader("token") String token) throws Exception {
        List<OrderVo> orderVos = orderService.selectWaitGet(token);
        System.out.println("等待收货的数据为："+orderVos);
        return new ResponseResult(200, "获取等待收货数据成功",orderVos);
    }
    //根据用户ID，获取我配送的
    @GetMapping("/selectQiPeiing")
    public ResponseResult selectQiPeiing(@RequestHeader("token") String token) throws Exception {
        List<OrderVo> orderVos = orderService.selectOrderInfoByQiId(token);
        System.out.println("等待收货的数据为："+orderVos);
        return new ResponseResult(200, "获取我配送的数据成功",orderVos);
    }
    //根据用户ID，所有属于我的已经完成的订单
    @GetMapping("/selectWanChengOrder")
    public ResponseResult selectOrderInfoByWanCheng(@RequestHeader("token") String token) throws Exception {
        List<OrderVo> orderVos = orderService.selectOrderInfoByWanCheng(token);
        System.out.println("等待收货的数据为："+orderVos);
        return new ResponseResult(200, "获取我已经完成的数据成功",orderVos);
    }
    //根据用户ID，所有属于我的订单
    @GetMapping("/selectAllOrderInfo")
    public ResponseResult selectAllOrderInfo(@RequestHeader("token") String token) throws Exception {
        List<OrderVo> orderVos = orderService.selectAllOrderInfo(token);
        System.out.println("等待收货的数据为："+orderVos);
        return new ResponseResult(200, "获取我的所有订单成功",orderVos);
    }
    //根据用户ID，所有属于我的售后中订单
    @GetMapping("/selectOrderInShouHou")
    public ResponseResult selectOrderInfoByShouHou(@RequestHeader("token") String token) throws Exception {
        List<OrderVo> orderVos = orderService.selectOrderInfoByShouHou(token);
        System.out.println("等待收货的数据为："+orderVos);
        return new ResponseResult(200, "获取我的所有订单成功",orderVos);
    }


    //根据订单ID修改订单得状态
    //我已到达：修改订单状态为：已送达 √
    //取消订单：修改订单状态为：已取消 并 从订单表中获取到任务ID，把该任务中得状态修改为待接单
    @PostMapping("/served")
    public ResponseResult served(@RequestHeader("token") String token,
                                 @RequestBody Map<String, Object> requestData) throws Exception {

//        Long orderid = Long.parseLong(orderid1);
//        Long taskid = Long.parseLong(taskid1);
        Long orderid = Long.parseLong(requestData.get("orderid").toString());
        Long taskid = Long.parseLong(requestData.get("taskid").toString());

        System.out.println("获取的两个数据是："+orderid+",第二个："+taskid+"token:"+token);
        int num = orderService.alreayArrived(token,orderid,taskid);
        if (num == -1) {
            return new ResponseResult(409,"订单已完成");
        }
        return new ResponseResult(200,"订单已完成");


    }


    //点击确认收货
    @PostMapping("/sureArrive")
    public ResponseResult sureArrival(@RequestHeader("token") String token,
                                      @RequestBody OrderVo orderVo) throws Exception {

        System.out.println("接收到的orderVo为："+orderVo);
        int num = orderService.sureArrive(token,orderVo);
        System.out.println("这个数字是："+num);
        if(num == 0){
            return new ResponseResult(400, "确认收货失败");
        }
        return new ResponseResult(200, "确认收货成功",num);
    }



    @GetMapping("/orderindex")
    public ResponseResult orderIndex(@RequestHeader("token") String token) throws Exception {

        Long[] ordercount = orderService.selectOrderCount(token);
        return new ResponseResult(200, "查询成功",ordercount);
    }








}
